<template>
  <div class="aside">
    <!-- 侧边栏 -->
    <el-aside width="isCollapse ? '64px' :'200px'">
      <div class="toggle-button" @click="toggleCollapse">❤❤❤</div>
      <!-- 侧边栏菜单区域 -->
      <el-menu
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#409EFF"
        :unique-opened="true"
        :collapse="isCollapse"
        :collapse-transition="false"
        :router="true"
        :default-active="activePath"
      >
        <el-submenu
          :index="item.id + ''"
          v-for="item in menulist"
          :key="item.id"
        >
          <!-- 一级菜单模板区域 -->
          <template slot="title">
            <!-- 图标 -->
            <i :class="iconObject[item.id]"></i>
            <!-- 文本 -->
            <span>{{ item.authName }}</span>
          </template>
          <!-- 二级菜单 -->
          <el-menu-item
            :index="'/' + subItem.path"
            v-for="subItem in item.children"
            :key="subItem.id"
            @click="saveActiveNav('/'+subItem.path)"
          >
            <!-- 二级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ subItem.authName }}</span>
            </template>
          </el-menu-item>
        </el-submenu>
      </el-menu></el-aside
    >
  </div>
</template>
<script>
export default {
  props: {
    menulist: {
      type: Array,
      required: true
    }
  },
  created() {
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      iconObject: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的连接地址
      activePath: ''
    }
  },
  methods: {
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveActiveNav(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.aside {
  height: 100%;
  background-color: #4a5064;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: 0;
  }
  .iconfont {
    margin-right: 10px;
  }
  .toggle-button {
    background-color: #4a5064;
    color: #fff;
    text-align: center;
    font-size: 10px;
    line-height: 24px;
  }
}
</style>
